package com.forum.module.blog.controller.favorite;

import cn.hutool.core.collection.CollUtil;
import com.forum.framework.common.pojo.DataResult;
import com.forum.framework.common.pojo.PageResult;
import com.forum.module.blog.controller.favorite.vo.FavoritePageReqVO;
import com.forum.module.blog.controller.favorite.vo.FavoriteRespVO;
import com.forum.module.blog.controller.favorite.vo.FavoriteSavaReqVO;
import com.forum.module.blog.convert.favorite.FavoriteConvert;
import com.forum.module.blog.dal.dataobject.favorite.FavoriteDO;
import com.forum.module.blog.service.favorite.FavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import static com.forum.framework.common.pojo.DataResult.success;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/4 19:18
 */
@Tag(name = "博客管理 - 收藏")
@RestController
@RequestMapping("/blog/favorite")
public class FavoriteController {
    private final FavoriteService favoriteService;

    public FavoriteController(FavoriteService favoriteService) {
        this.favoriteService = favoriteService;
    }

    @PostMapping("/create")
    @Operation(summary = "新增收藏")
    public DataResult<Long> createFavorite(@RequestBody FavoriteSavaReqVO reqVO) {
        Long id = favoriteService.createFavorite(reqVO);
        return success(id);
    }

    @PostMapping("/update")
    @Operation(summary = "更新收藏")
    public DataResult<Boolean> updateFavorite(@RequestBody FavoriteSavaReqVO reqVO) {
        favoriteService.updateFavorite(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收藏信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public DataResult<Boolean> deleteFavorite(@RequestParam("id") Long id) {
        favoriteService.deleteById(id);
        return success(true);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询收藏列表")
    public DataResult<PageResult<FavoriteRespVO>> page(@RequestBody @Valid FavoritePageReqVO pageReqVO) {
        // 获得文章分页列表
        PageResult<FavoriteDO> pageResult = favoriteService.getFavoritePage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }

        return success(new PageResult<>(FavoriteConvert.INSTANCE.covertList(pageResult.getList()), pageResult.getTotal()));
    }
}
